// pages/product/product.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loadmore:'block',
    nomore:'none',
    nodata:'none',
    page:1,
    list:[],
    category1id: 0,
    category2id: 0,
    tags:'',
    order:'allrank',
    desc:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var order = "allrank";
    var tags = "";
    var category1id = 0;
    var category2id = 0;
    if (options.order){
      order = options.order;
    }
    if (options.tags) {
      tags = options.tags;
    }
    if (options.category1id) {
      category1id = options.category1id;
    }
    if (options.category2id) {
      category2id = options.category2id;
    }
    var desc="";
    if(order=="aprice"){
      desc="asc";
    }
    this.setData({ order: order, desc: desc, tags:tags, category1id: category1id, category2id: category2id});
  },
  onShow: function (){
    if(app.IsFlash){
      this.setData({ list: [], page: 1, loadmore: 'block', nomore: 'none', nodata:'none' });
      this.Find_Product();
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.page>1){  //必须加上，否则上拉和下拉会同时触发
      this.Find_Product();
    }
  },
  openSearch: function (e) {
    var order = e.currentTarget.dataset.order;
    this.setData({order: order});
    wx.navigateTo({
      url: '/pages/search/search?froms=product'
    })
  },
  Find_Product: function () {
    var that = this;
    var data = {};
    data.act = "Find_Product";
    data.order=that.data.order;
    data.desc = that.data.desc;
    data.page = that.data.page;
    data.tags = that.data.tags;
    data.category1id = that.data.category1id;
    data.category2id = that.data.category2id;
    if (app.Keyword!=""){
      data.keyword=app.Keyword;
    }
    app.Ajax(data, function (json) {
      var pagecount=Math.floor(Math.abs(json.count-1)/json.pagesize)+1;
      if(data.page>pagecount){return;}
      that.setData({list: that.data.list.concat(json.data), page: data.page + 1});
      if (that.data.list.length == 0) {
        that.setData({ loadmore: 'none', nodata: 'block', nomore: 'none' });
      }
      else if (pagecount == 1) {
        that.setData({ loadmore: 'none', nodata: 'none', nomore: 'none' });
      }
      else if (data.page < pagecount) {
        that.setData({ loadmore: 'block', nodata: 'none', nomore: 'none' });
      } else if (data.page == pagecount) {
        that.setData({ loadmore: 'none', nodata: 'none', nomore: 'block' });
      }
      wx.stopPullDownRefresh();
    });
  },
  Find_Category: function () {
    var that = this;
    var data = {};
    data.act = "Find_Category";    
    app.Ajax(data, function (json) {
      that.setData({ category1: json.data.category1, category2: json.data.category2});
    });
  },
  openProductInfo: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/productinfo/productinfo?id='+id
    })
  },
  openReflash:function(e){
    var order = e.currentTarget.dataset.order;
    var desc = e.currentTarget.dataset.desc;
    if(order=="aprice"){
      if (desc == "") {
        desc = "asc";
      } 
      else if(desc=="desc"){
        desc="asc";
      } 
      else if (desc == "asc"){
        desc="desc";
      }
    }else{
      desc = "";
    }
    this.setData({ list: [], page: 1, loadmore: 'block', nomore: 'none', nodata:'none', order: order, desc:desc });
    this.Find_Product();
  }
})